package com.atguigu.lease.web.app.service.impl;

//import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    public AttrValueMapper attrValueMapper;
    @Autowired
    public  AttrKeyMapper attrKeyMapper;
    @Autowired
    public  FacilityInfoMapper facilityInfoMapper;
    @Autowired
    public  LabelInfoMapper labelInfoMapper;
    @Autowired
    public  PaymentTypeMapper paymentTypeMapper;
    @Autowired
    public LeaseTermMapper leaseTermMapper;

    @Override
    public RoomDetailVo getRoomInfoById(Long id) {
        RoomDetailVo roomDetailVo =new RoomDetailVo();
        //room基本信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        roomDetailVo.setId(roomInfo.getId());
        roomDetailVo.setCreateTime(roomInfo.getCreateTime());
        roomDetailVo.setUpdateTime(roomInfo.getUpdateTime());
        roomDetailVo.setRent(roomInfo.getRent());
        roomDetailVo.setIsRelease(roomInfo.getIsRelease());
        //照片信息
        QueryWrapper<GraphInfo> graphInfoQueryWrapper=new QueryWrapper<>();
        graphInfoQueryWrapper.eq("item_type",2);
        graphInfoQueryWrapper.eq("item_id",roomInfo.getId());
        List<GraphInfo> graphInfoList = graphInfoService.list(graphInfoQueryWrapper);
        List<GraphVo> graphInfos= new ArrayList<>();
        for (GraphInfo graphInfo : graphInfoList) {
            GraphVo graphVo=new GraphVo();
            graphVo.setName(graphInfo.getName());
            graphVo.setUrl(graphInfo.getUrl());
            graphInfos.add(graphVo);
        }
        roomDetailVo.setGraphVoList(graphInfos);

        //房间基本信息
        List<AttrValueVo> attrValueVoList=new ArrayList<>();

        //查询attr的id
        QueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper =new QueryWrapper<>();
        roomAttrValueQueryWrapper.eq("room_id",id);
        List<RoomAttrValue> roomAttrValues = roomAttrValueService.list(roomAttrValueQueryWrapper);
        for (RoomAttrValue roomAttrValue : roomAttrValues) {
            AttrValueVo attrValueVo=new AttrValueVo();
            attrValueVo.setId(roomAttrValue.getId());
            attrValueVo.setCreateTime(roomAttrValue.getCreateTime());
            attrValueVo.setUpdateTime(roomAttrValue.getUpdateTime());
            AttrValue attrValue = attrValueMapper.selectById(roomAttrValue.getAttrValueId());

            attrValueVo.setName(attrValue.getName());
            attrValueVo.setAttrKeyId(attrValue.getAttrKeyId());
            AttrKey attrKey = attrKeyMapper.selectById(attrValue.getAttrKeyId());
            attrValueVo.setAttrKeyName(attrKey.getName());

            attrValueVoList.add(attrValueVo);
        }

        roomDetailVo.setAttrValueVoList(attrValueVoList);

        //房间设备信息
        QueryWrapper<RoomFacility> roomFacilityQueryWrapper=new QueryWrapper<RoomFacility>();
        roomFacilityQueryWrapper.eq("room_id", roomInfo.getId());
        List<RoomFacility> facilities = roomFacilityService.list(roomFacilityQueryWrapper);
        List facilityIds=new ArrayList();
        for (RoomFacility facility : facilities) {
            facilityIds.add(facility.getFacilityId());
        }
        List list = facilityInfoMapper.selectBatchIds(facilityIds);
        roomDetailVo.setFacilityInfoList(list);


        //房间标签信息
        QueryWrapper<RoomLabel> roomLabelQueryWrapper =new QueryWrapper<>();
        roomLabelQueryWrapper.eq("room_id", id);
        List<RoomLabel> labels = roomLabelService.list(roomLabelQueryWrapper);
        List labelIds=new ArrayList<>();
        for (RoomLabel label : labels) {
            labelIds.add(label.getLabelId());
        }
        List list1 = labelInfoMapper.selectBatchIds(labelIds);
        roomDetailVo.setLabelInfoList(list1);

        //支付方式信息
        QueryWrapper<RoomPaymentType> paymentTypeQueryWrapper=new QueryWrapper<>();
        paymentTypeQueryWrapper.eq("room_id",id);
        List<RoomPaymentType> paymentTypes = roomPaymentTypeService.list(paymentTypeQueryWrapper);
        List paymentIds =new ArrayList<>();
        for (RoomPaymentType paymentType : paymentTypes) {
            paymentIds.add(paymentType.getPaymentTypeId());
        }
        List list2 = paymentTypeMapper.selectBatchIds(paymentIds);
        roomDetailVo.setPaymentTypeList(list2);
        //租期信息
        QueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper= new QueryWrapper<>();
        roomLeaseTermQueryWrapper.eq("room_id",id);
        List<RoomLeaseTerm> roomLeaseTerms = roomLeaseTermService.list(roomLeaseTermQueryWrapper);
        List leaseIds =new ArrayList<>();
        for (RoomLeaseTerm roomLeaseTerm : roomLeaseTerms) {
            leaseIds.add(roomLeaseTerm.getLeaseTermId());
        }
        List list3 = leaseTermMapper.selectBatchIds(leaseIds);
        roomDetailVo.setLeaseTermList(list3);

        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> getPageItemByCondition(IPage<RoomItemVo> page,long current,  long size, RoomQueryVo queryVo) {
        QueryWrapper<ApartmentInfo> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq(queryVo.getProvinceId()!=null,"province_id",queryVo.getProvinceId());
        queryWrapper.eq(queryVo.getCityId()!=null,"province_id",queryVo.getCityId());
        queryWrapper.eq(queryVo.getDistrictId()!=null,"province_id",queryVo.getDistrictId());
        List<ApartmentInfo> apartmentInfos = apartmentInfoMapper.selectList(queryWrapper);


        IPage<RoomInfo> roomInfoPage=new Page<>(current,size);
        QueryWrapper<RoomInfo> queryWrapper1=new QueryWrapper<>();
        for (ApartmentInfo apartmentInfo : apartmentInfos) {
            queryWrapper1.eq("apartment_id",apartmentInfo.getId());
            queryWrapper1.or();
        }
        IPage<RoomInfo> roomInfoIPage = roomInfoMapper.selectPage(roomInfoPage, queryWrapper1);
        //List<RoomInfo> roomInfos = roomInfoMapper.selectList(queryWrapper1);
        List<RoomInfo> roomInfos = roomInfoIPage.getRecords();

        List<RoomItemVo> roomItemVos= new ArrayList<>();
        for (RoomInfo roomInfo : roomInfos) {
            RoomItemVo roomItemVo=new RoomItemVo();
            roomItemVo.setId(roomInfo.getId());
            roomItemVo.setRoomNumber(roomInfo.getRoomNumber());
            roomItemVo.setRent(roomInfo.getRent());
            LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(roomInfo.getId());
            roomItemVos.add(roomItemVo);
        }
        page.setRecords(roomItemVos);
        page.setPages(roomInfoIPage.getPages());
        page.setCurrent(roomInfoIPage.getCurrent());
        page.setSize(roomInfoIPage.getSize());
        page.setTotal(roomInfoIPage.getTotal());
        return page;
    }

}




